<?php
/**
 * @package      OWNServices
 * @subpackage   Site
 *
 * @author       Sanawin Tinsuwan	
 * @copyright    Copyright (C) 20014 Sanawin Tinsuwan. All rights reserved.
 * @license      http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
 */

defined('_JEXEC') or die();

/**
 * Helper class for Ownservices.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_ownservices
 * @since       1.6
 */
class OWNServicesHelper
{
	/**
	 * Get branch list in text/value format for a select field
	 *
	 * @return  array
	 */
	public static function getBranchOptions()
	{
		$options = array();

		$db		= JFactory::getDbo();
		$user	= JFactory::getUser();

		// Construct the query
		$query = $db->getQuery(true)
			->select('b.id AS value, b.name AS text')
			->from('#__own_branches AS b') 
			->join('LEFT', '#__own_users AS u ON u.branchid=b.id');
		
		if(!$user->authorise('core.admin'))
		{
			$query->where('u.userid='.(int) $user->get('id'));
		}

		// Group by ID, Name
		$query->group('b.id, b.name');
		
		// Add the list ordering clause.
		$query->order('b.name');

		// Setup the query
		$db->setQuery($query);

		try
		{
			$options = $db->loadObjectList();
		}
		catch (RuntimeException $e)
		{
			JError::raiseWarning(500, $e->getMessage());
		}

		// Merge any additional options in the XML definition.
		// $options = array_merge(parent::getOptions(), $options);

		// array_unshift($options, JHtml::_('select.option', '0', JText::_('COM_OWNSERVICES_NO_BRANCH')));

		return $options;
	}
}